
package forum.client.ui;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import forum.client.controllerlayer.ControllerHandler;
import forum.client.controllerlayer.ControllerHandlerFactory;

public class UsersList extends javax.swing.JDialog implements UsersListHandlerInt{
	private static final long serialVersionUID = 5082370938099703037L;
	private boolean demotePromote;
	private ExecutorService m_pool;
	/** Creates new form UsersList */

	/**
	 * A pipe interface to communicate with the controller layer.
	 */
	private ControllerHandler m_pipe;
    public UsersList(java.awt.Frame parent, boolean modal) {
    	super(parent, modal);
    	this.m_pool = Executors.newCachedThreadPool();
        this.m_pipe=ControllerHandlerFactory.getPipe();
        initComponents();
        this.m_pipe.addObserver(new UsersListObserver(this));
        demotionModel=new UsersListModelDemotion();
        promotionModel=new UsersListModelPromotion();
        demotePromote=false;
    }

	@Override
	public synchronized void demoteUsers(String[] users) {
		this.m_pipe.demoteUsers(users);
	}

	@Override
	public synchronized String[] getUsersForDemotion() {
		return this.m_pipe.getUsersToDemote();
	}

	@Override
	public synchronized String[] getUsersForPromotion() {
		return this.m_pipe.getUsersToPromote();
	}

	@Override
	public synchronized void promoteUsers(String[] users) {
		this.m_pipe.promoteUsers(users);
	}

	@Override
	public synchronized void NotifyError(String errorMessage) {
		JFrame frame = new JFrame();
		JOptionPane.showMessageDialog(frame,
			    errorMessage,
			    "Operation failed.",
			    JOptionPane.WARNING_MESSAGE);
	}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jScrollPane1.setViewportView(jList1);

        jButton1.setText("OK");
        jButton1.setMaximumSize(new java.awt.Dimension(90, 23));
        jButton1.setMinimumSize(new java.awt.Dimension(90, 23));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        
        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                        .addGap(30, 30, 30)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		setVisible(false);
	}
	
	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		m_pool.execute(new SwingWorker<Void, Void>() {
            public Void doInBackground() {
            	int[] selected=jList1.getSelectedIndices();
        		String[] users=new String[selected.length];
        		for(int i=0;i<selected.length;i++)
        			users[i]=(String) jList1.getModel().getElementAt(selected[i]);
        		if(demotePromote) m_pipe.promoteUsers(users);
        		else m_pipe.demoteUsers(users);
        		return null;
            }
        });
		setVisible(false);
	}

	/**
	 *
	 * @param demotPromote true - init promotion,false - init demotion.
	 */
	public void setListItems(boolean demotPromote){
		this.demotePromote=demotPromote;
		if(demotPromote){
			jButton1.setText("Promote");
			promotionModel.reset();
			jList1.setModel(promotionModel);
		}
		else{
			jButton1.setText("Demote");
			demotionModel.reset();
			jList1.setModel(demotionModel);
		}

	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private UsersListModelDemotion demotionModel;
    private UsersListModelPromotion promotionModel;
    // End of variables declaration//GEN-END:variables

}


class UsersListModelDemotion extends javax.swing.AbstractListModel{
	private static final long serialVersionUID = 9062500371384253902L;
	private ControllerHandler m_pipe;
	private String[] strings;
	public UsersListModelDemotion(){
		this.m_pipe=ControllerHandlerFactory.getPipe();
		strings=new String[1];
	}
	public void reset(){
		strings = this.m_pipe.getUsersToDemote();
	}
    public int getSize() { 
    	if(strings==null) return 0;
    	return strings.length; 
    }
    public Object getElementAt(int i) { return strings[i]; }
}

class UsersListModelPromotion extends javax.swing.AbstractListModel{
	private static final long serialVersionUID = 1072747374229867990L;
	private ControllerHandler m_pipe;
	private String[] strings;
	public UsersListModelPromotion(){
		this.m_pipe=ControllerHandlerFactory.getPipe();
		strings=new String[1];
	}
	public void reset(){
		strings = this.m_pipe.getUsersToPromote();
	}
    public int getSize() { 
    	if(strings==null) return 0;
    	return strings.length; 
    }
    public Object getElementAt(int i) { return strings[i]; }
}